<html>
<head><meta charset="utf-8"><title>unreachable warning for default impl in private traits · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/unreachable.20warning.20for.20default.20impl.20in.20private.20traits.html">unreachable warning for default impl in private traits</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="221224306"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/unreachable%20warning%20for%20default%20impl%20in%20private%20traits/near/221224306" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elazar Leibovich <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/unreachable.20warning.20for.20default.20impl.20in.20private.20traits.html#221224306">(Dec 30 2020 at 16:23)</a>:</h4>
<p>Hi,<br>
With the help of <span class="user-mention" data-user-id="232545">@Joshua Nelson</span> I thought to try and add warning for unreachable default implementation in private traits.</p>
<p>I started by following the guide and compiling + checking rust with <code>x.py</code>.</p>
<p>What should be the next step?</p>



<a name="221224396"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/unreachable%20warning%20for%20default%20impl%20in%20private%20traits/near/221224396" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/unreachable.20warning.20for.20default.20impl.20in.20private.20traits.html#221224396">(Dec 30 2020 at 16:24)</a>:</h4>
<p>(previously: <a href="#narrow/stream/122651-general/topic/unused.20fn.20that.20is.20used.20in.20trait">https://rust-lang.zulipchat.com/#narrow/stream/122651-general/topic/unused.20fn.20that.20is.20used.20in.20trait</a>)</p>



<a name="221224425"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/unreachable%20warning%20for%20default%20impl%20in%20private%20traits/near/221224425" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/unreachable.20warning.20for.20default.20impl.20in.20private.20traits.html#221224425">(Dec 30 2020 at 16:24)</a>:</h4>
<p>I would start by finding where the existing <code>dead_code</code> warnings are emitted</p>



<a name="221224433"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/unreachable%20warning%20for%20default%20impl%20in%20private%20traits/near/221224433" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/unreachable.20warning.20for.20default.20impl.20in.20private.20traits.html#221224433">(Dec 30 2020 at 16:24)</a>:</h4>
<p>and see if traits are being special cased</p>



<a name="221224445"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/unreachable%20warning%20for%20default%20impl%20in%20private%20traits/near/221224445" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/unreachable.20warning.20for.20default.20impl.20in.20private.20traits.html#221224445">(Dec 30 2020 at 16:25)</a>:</h4>
<p>if so this will be relatively easy and you just have to remove the special casing</p>



<a name="221224498"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/unreachable%20warning%20for%20default%20impl%20in%20private%20traits/near/221224498" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elazar Leibovich <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/unreachable.20warning.20for.20default.20impl.20in.20private.20traits.html#221224498">(Dec 30 2020 at 16:26)</a>:</h4>
<p>In <code>src/tools/rust-analyzer</code>?</p>



<a name="221224541"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/unreachable%20warning%20for%20default%20impl%20in%20private%20traits/near/221224541" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/unreachable.20warning.20for.20default.20impl.20in.20private.20traits.html#221224541">(Dec 30 2020 at 16:26)</a>:</h4>
<p>no, in <code>compiler/</code></p>



<a name="221224550"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/unreachable%20warning%20for%20default%20impl%20in%20private%20traits/near/221224550" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/unreachable.20warning.20for.20default.20impl.20in.20private.20traits.html#221224550">(Dec 30 2020 at 16:26)</a>:</h4>
<p>rust-analyzer is a different code base, that's used for IDEs</p>



<a name="221224920"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/unreachable%20warning%20for%20default%20impl%20in%20private%20traits/near/221224920" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elazar Leibovich <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/unreachable.20warning.20for.20default.20impl.20in.20private.20traits.html#221224920">(Dec 30 2020 at 16:31)</a>:</h4>
<div class="codehilite"><pre><span></span><code>impl Visitor&lt;&#39;tcx&gt; for DeadVisitor&lt;&#39;tcx&gt; {
...
    // Overwrite so that we don&#39;t warn the trait item itself.
    fn visit_trait_item(&amp;mut self, trait_item: &amp;&#39;tcx hir::TraitItem&lt;&#39;tcx&gt;) {
        match trait_item.kind {
            hir::TraitItemKind::Const(_, Some(body_id))
            | hir::TraitItemKind::Fn(_, hir::TraitFn::Provided(body_id)) =&gt; {
                self.visit_nested_body(body_id)
            }
            hir::TraitItemKind::Const(_, None)
            | hir::TraitItemKind::Fn(_, hir::TraitFn::Required(_))
            | hir::TraitItemKind::Type(..) =&gt; {}
        }
    }
}
</code></pre></div>
<p>This seems to imply that traits are special cased.</p>



<a name="221224972"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/unreachable%20warning%20for%20default%20impl%20in%20private%20traits/near/221224972" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/unreachable.20warning.20for.20default.20impl.20in.20private.20traits.html#221224972">(Dec 30 2020 at 16:32)</a>:</h4>
<p>there you go then :) just delete that function</p>



<a name="221224983"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/unreachable%20warning%20for%20default%20impl%20in%20private%20traits/near/221224983" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elazar Leibovich <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/unreachable.20warning.20for.20default.20impl.20in.20private.20traits.html#221224983">(Dec 30 2020 at 16:32)</a>:</h4>
<p>Just for private traits.</p>



<a name="221225002"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/unreachable%20warning%20for%20default%20impl%20in%20private%20traits/near/221225002" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elazar Leibovich <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/unreachable.20warning.20for.20default.20impl.20in.20private.20traits.html#221225002">(Dec 30 2020 at 16:32)</a>:</h4>
<p>But I don't understand why it works, it seems we do visit the nested body of the trait</p>



<a name="221225041"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/unreachable%20warning%20for%20default%20impl%20in%20private%20traits/near/221225041" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elazar Leibovich <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/unreachable.20warning.20for.20default.20impl.20in.20private.20traits.html#221225041">(Dec 30 2020 at 16:33)</a>:</h4>
<p>So how come the overriden function disable the warning?</p>



<a name="221225051"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/unreachable%20warning%20for%20default%20impl%20in%20private%20traits/near/221225051" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/unreachable.20warning.20for.20default.20impl.20in.20private.20traits.html#221225051">(Dec 30 2020 at 16:33)</a>:</h4>
<p>ok, then you can check if it's public with <code>tcx.visibility(trait_item.def_id)</code>: <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/context/struct.TyCtxt.html#method.visibility">https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/context/struct.TyCtxt.html#method.visibility</a></p>



<a name="221225056"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/unreachable%20warning%20for%20default%20impl%20in%20private%20traits/near/221225056" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/unreachable.20warning.20for.20default.20impl.20in.20private.20traits.html#221225056">(Dec 30 2020 at 16:33)</a>:</h4>
<p><span class="user-mention silent" data-user-id="374319">Elazar Leibovich</span> <a href="#narrow/stream/182449-t-compiler.2Fhelp/topic/unreachable.20warning.20for.20default.20impl.20in.20private.20traits/near/221225002">said</a>:</p>
<blockquote>
<p>But I don't understand why it works, it seems we do visit the nested body of the trait</p>
</blockquote>
<p>the body is not the same as the item itself</p>



<a name="221225063"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/unreachable%20warning%20for%20default%20impl%20in%20private%20traits/near/221225063" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/unreachable.20warning.20for.20default.20impl.20in.20private.20traits.html#221225063">(Dec 30 2020 at 16:33)</a>:</h4>
<p>that will warn about unused variables in the function, but not about the function itself being used</p>



<a name="221275288"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/unreachable%20warning%20for%20default%20impl%20in%20private%20traits/near/221275288" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elazar Leibovich <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/unreachable.20warning.20for.20default.20impl.20in.20private.20traits.html#221275288">(Dec 31 2020 at 07:41)</a>:</h4>
<p>How do I get <code>def_id</code> from <code>trait_item</code>?</p>



<a name="221281314"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/unreachable%20warning%20for%20default%20impl%20in%20private%20traits/near/221281314" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elazar Leibovich <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/unreachable.20warning.20for.20default.20impl.20in.20private.20traits.html#221281314">(Dec 31 2020 at 10:19)</a>:</h4>
<div class="codehilite"><pre><span></span><code>error[E0599]: no method named `def_id` found for reference `&amp;&#39;tcx rustc_hir::TraitItem&lt;&#39;tcx&gt;` in the current scope
   --&gt; compiler/rustc_passes/src/dead.rs:685:43
    |
685 |         if self.tcx.visibility(trait_item.def_id()) {
    |                                           ^^^^^^ method not found in `&amp;&#39;tcx rustc_hir::TraitItem&lt;&#39;tcx&gt;`
</code></pre></div>



<a name="221282046"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/unreachable%20warning%20for%20default%20impl%20in%20private%20traits/near/221282046" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Léo Lanteri Thauvin <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/unreachable.20warning.20for.20default.20impl.20in.20private.20traits.html#221282046">(Dec 31 2020 at 10:35)</a>:</h4>
<p>Maybe use the HIR map?</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">if</span><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="n">tcx</span><span class="p">.</span><span class="n">visibility</span><span class="p">(</span><span class="bp">self</span><span class="p">.</span><span class="n">tcx</span><span class="p">.</span><span class="n">hir</span><span class="p">().</span><span class="n">local_def_id</span><span class="p">(</span><span class="n">trait_item</span><span class="p">.</span><span class="n">hir_id</span><span class="p">))</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
</code></pre></div>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>